<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="服务类型" prop="projectId"  >
        <el-select
          v-model="formData.projectId"
          class="!w-240px"
          placeholder="请选择服务类型"
        >
          <el-option
            v-for="(item) in projectList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入服务名称"  class="!w-240px" />
      </el-form-item>
      <el-form-item label="服务图标" prop="icon">
        <UploadImg v-model="formData.icon" />
        <span>宽170高170</span>
      </el-form-item>
      <el-form-item label="服务背景图" prop="cover">
        <UploadImg v-model="formData.cover" />
      </el-form-item>
      <el-form-item label="服务内容" prop="serviceContent">
        <UploadImg v-model="formData.serviceContent" />
      </el-form-item>
      <el-form-item label="简要描述" prop="recommend">
        <el-input v-model="formData.recommend" placeholder="请输入介绍" />
      </el-form-item>
      <el-form-item label="现价" prop="price">
        <el-input-number v-model="formData.price" :min="0" placeholder="请输入现价" class="!w-240px" />
      </el-form-item>
      <el-form-item label="原价" prop="originalPrice">
        <el-input-number v-model="formData.originalPrice" :min="0" placeholder="请输入原价" class="!w-240px"  />
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <el-input v-model="formData.label" placeholder="请输入标签，使用(,)号分割" />
      </el-form-item>
      <el-form-item label="员工分成比例" prop="staffProportion"  labelWidth="150px" >
        <el-input v-model="formData.staffProportion" :min="0" :max="100" placeholder="请输入员工收入比例,80代表80%" class="!w-240px"  />
      </el-form-item>
      <el-form-item label="显示原价" prop="isPrice" labelWidth="150px">
        <el-radio-group v-model="formData.isPrice">
          <el-radio :label=1>是</el-radio>
          <el-radio :label=0>否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OpenCityApi } from '@/api/peizhen/opencity'
import {ProjectApi} from "@/api/peizhen/project";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  icon: undefined,
  cover: undefined,
  provinceCode: undefined,
  province: undefined,
  cityCode: undefined,
  city: undefined,
  recommend: undefined,
  serviceContent: undefined,
  originalPrice: undefined,
  label: undefined,
  price: undefined,
  projectId: undefined,
  staffProportion: undefined,
  isPrice: undefined,
})
const formRules = reactive({
  projectId: [{ required: true, message: '请选择服务类型', trigger: 'blur' }],
  name: [{ required: true, message: '服务名称不能为空', trigger: 'blur' }],
  icon: [{ required: true, message: '服务图标不能为空', trigger: 'blur' }],
  cover: [{ required: true, message: '背景图不能为空', trigger: 'blur' }],
  cityCode: [{ required: true, message: '市编码不能为空', trigger: 'change' }],
  price: [{ required: true, message: '现价不能为空', trigger: 'blur' }],
  staffProportion: [{ required: true, message: '员工分成比例不能空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
//项目类型
const projectList=ref([
  {id:0,
  name:""}
])

/** 打开弹窗 */
const open = async (type: string, id?: number, cityCode: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.cityCode = cityCode
  //获取服务类型
  projectList.value= await ProjectApi.getProjectList()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OpenCityApi.getProgram(id)
      formData.value.price=formData.value.price/100
      formData.value.originalPrice=formData.value.originalPrice/100
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await OpenCityApi.createProgram(data)
      message.success(t('common.createSuccess'))
    } else {
      await OpenCityApi.updateProgram(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    icon: undefined,
    cover: undefined,
    provinceCode: undefined,
    province: undefined,
    cityCode: undefined,
    city: undefined,
    recommend: undefined,
    serviceContent: undefined,
    originalPrice: undefined,
    label: undefined,
    price: undefined,
    projectId: undefined,
    staffProportion: undefined,
    isPrice: undefined,
  }
  formRef.value?.resetFields()
}
</script>
